package com.newer.service.sys.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.newer.dao.sys.mapper.OrganizationMapper;
import com.newer.dao.sys.mapper.StaffMapper;
import com.newer.pojo.sys.Organization;
import com.newer.pojo.sys.Staff;
import com.newer.pojo.sys.StaffExample;
import com.newer.pojo.sys.StaffExample.Criteria;
import com.newer.service.sys.iter.StaffService;
import com.newer.utils.sys.BootstrapDataTable;
import com.newer.utils.sys.Conditions;
import com.newer.utils.sys.IsNotNullUtil;
import com.newer.utils.sys.MsgUtil;

/**
 * service层
 * 
 * @author yuangxinghui
 *
 */
@Service("staffService")
public class StaffServiceImpl implements StaffService {

	@Autowired
	private StaffMapper staffMapper;
	
	@Autowired
	private OrganizationMapper organizationMapper;

	@Override
	public int countByExample(StaffExample example) {
		// TODO Auto-generated method stub
		return staffMapper.countByExample(example);
	}

	@Override
	public int deleteByExample(StaffExample example) {
		// TODO Auto-generated method stub
		return staffMapper.deleteByExample(example);
	}

	@Override
	public int deleteByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		return staffMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(Staff record) {
		// TODO Auto-generated method stub
		return staffMapper.insert(record);
	}

	@Override
	public int insertSelective(Staff record) {
		// TODO Auto-generated method stub
		return staffMapper.insertSelective(record);
	}

	@Override
	public List<Staff> selectByExample(StaffExample example) {
		// TODO Auto-generated method stub
		return staffMapper.selectByExample(example);
	}

	@Override
	public Staff selectByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		return staffMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByExampleSelective(Staff record, StaffExample example) {
		// TODO Auto-generated method stub
		return staffMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int updateByExample(Staff record, StaffExample example) {
		// TODO Auto-generated method stub
		return staffMapper.updateByExample(record, example);
	}

	@Override
	public int updateByPrimaryKeySelective(Staff record) {
		// TODO Auto-generated method stub
		return staffMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(Staff record) {
		// TODO Auto-generated method stub
		return staffMapper.updateByPrimaryKey(record);
	}

	/**
	 * 查询所有状态为0的取派员
	 */
	@Override
	public BootstrapDataTable selectStaffByExample(BootstrapDataTable dataTable, Conditions conditions) {
		StaffExample example = new StaffExample();
		Criteria createCriteria1 = example.createCriteria();
		Criteria createCriteria2 = example.createCriteria();
		// 设置初始排序规则
		if (IsNotNullUtil.isNotNull(example)) {
			example.setOrderByClause(dataTable.getSort() + "  " + dataTable.getOrder());
		}

		// 根据条件查询
		if (IsNotNullUtil.isNotNull(conditions)) {
			if (IsNotNullUtil.isNotNull(conditions.getSearch())) {
				createCriteria1.andTelephoneEqualTo(conditions.getSearch()).andDeltagEqualTo("0");
			}
			if (IsNotNullUtil.isNotNull(conditions.getSearch())) {
				createCriteria2.andStaffNameLike("%"+(conditions.getSearch()+"%")).andDeltagEqualTo("0");
			}
			//or 选择
			example.or(createCriteria2);
		}
		
		createCriteria1.andDeltagEqualTo("0");

		// 设置分页条件
		PageHelper.offsetPage(dataTable.getOffset(), dataTable.getLimit());

		List<Staff> staffList = staffMapper.selectByExample(example);
		PageInfo<Staff> info = new PageInfo<Staff>(staffList);

		// 设置一页显示几条数据
		dataTable.setRows(info.getList());
		// 设置总共多少条记录数
		dataTable.setTotal(new Long(info.getTotal()).intValue());
		return dataTable;
	}

	/**
	 * 取派员作废,实际上是修改取派员状态
	 */
	@Override
	public MsgUtil deleteStaff(Staff staff) {
		Staff staffs = new Staff();
		staffs.setDeltag("1");
		staffs.setId(staff.getId());
		try {
			if (staffMapper.updateByPrimaryKey(staffs) > 0) {
				MsgUtil.result(true, "执行成功");
			}
		} catch (Exception e) {
			MsgUtil.result(false, "系统繁忙，请稍后重试！");
		}
		return MsgUtil.result(false, "系统繁忙，请稍后重试！");
	}

	
	
	/**
	 * 查询所有部门
	 */
	@Override
	public List<Organization> selectAllOrganization() {
		return  organizationMapper.selectByExample(null);
	}

	/**
	 * 批量删除取派员信息
	 */
	@Override
	public int deleteStaffByIds(String Ids) {
		//截取
        String[] split = Ids.split(",");
 		//实例化集合
 		List<String> ids = null;
 		//添加删除id
         if(IsNotNullUtil.isNotNull(split)){
        	 ids=new ArrayList<>();
             for (String string : split) {
     			ids.add(string);
     		} 
         }
         //删除
		return staffMapper.deleteByIds(ids);
	}

}
